package cn.ssj.controller;

import cn.ssj.entity.Carousel;
import cn.ssj.service.CarouselService;
import cn.ssj.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2017/9/4 0004.
 */
@RestController
@EnableAutoConfiguration
public class CarouselController extends BaseController<Carousel,Integer>{

    @Autowired
    private CarouselService service;

    @RequestMapping("/back/addCarousel")
    public String add(Carousel entity) {
        entity.setPicture(super.filePath);
        return super.add(entity);
    }

    @RequestMapping("/back/updateCarousel")
    public String update(Carousel entity) {
        entity.setPicture(super.filePath);
        return super.update(entity);
    }

    @RequestMapping("/back/deleteCarousel")
    public String delete(@RequestParam("ids[]") Integer[] ids) {
        return super.delete(ids);
    }

    @RequestMapping("/back/queryAllCarouselByPage")
    public String queryAllByPage(Integer pageNumber) {
        return super.queryAllByPage(pageNumber);
    }

    @RequestMapping("/back/carouselUpload")
    public String upload(MultipartFile file, HttpServletRequest request) {
        super.setLocationPath(getLocationPath()+ "carousel");
        super.setHandlerPath(getHandlerPath()+"carousel");
        return super.upload(file,request);
    }
    @RequestMapping("/back/queryCarouselByTarget")
    public String queryByTarget(String target){
        if(null==target){
            return "error";
        }
        List<Carousel> list=service.queryByTargetAndUseFlag(target,USE_FLAG_NORMAL);
        if(list==null || list.size()==0){
            return "error";
        }
        return JSONUtil.objctToJSONArray(list,null);
    }
}
